package it.codelair.bakekaservice.contentProvider;

import it.codelair.bakekaservice.Utilities.sqllitemanager.DBHelper;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class EntiProvider extends ContentProvider{
	private static final String AUTHORITY = "it.codelair.bakekaservice.contentProvider"; //ho aggiunto contentprovider alla fine per precisione
	private static final String ENTI_BASE_PATH = DBHelper.TABLE_NAME; //la nostra risorsa, l'ho fissata uguale al nome della tabella anche se avrei potuto scegliere un nome qualsiasi
	public static final Uri ENTI_CONTENT_URI = Uri.parse("content://" + AUTHORITY+"/" + ENTI_BASE_PATH); //ecco l'uri base che punta alla risorsa tabella!
	
	public static final int ENTI = 100; //con 100 identifichiamo l'uri content://authority/tabella
	public static final int ENTI_ID = 110; //con 110 l'uri content://authority/tabella/id

	private static final UriMatcher sURIMatcher = new UriMatcher(
	                        UriMatcher.NO_MATCH);
    static {
            sURIMatcher.addURI(AUTHORITY, ENTI_BASE_PATH, ENTI); //colleghiamo gli uri ai tipi, es. /notes = 100
            sURIMatcher.addURI(AUTHORITY, ENTI_BASE_PATH + "/#", ENTI_ID);
    }
    
    public static final String ENTI_CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.codelair.listaEnti";
    public static final String ENTI_CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.codelair.ente";
    private SQLiteDatabase db;
	    
	@Override
	public String getType(Uri uri) {
		switch(sURIMatcher.match(uri)){ //per adesso supportiamo solo le note
        case ENTI: 
                return EntiProvider.ENTI_CONTENT_TYPE;
        case ENTI_ID:
                return EntiProvider.ENTI_CONTENT_ITEM_TYPE;
        default:
                throw new IllegalArgumentException("Invalid Uri!");
        }
	}
	
	@Override
	public boolean onCreate() {
		setDb(DBHelper.getHelper(getContext()).getWritableDatabase());
        return true;
	}
	
	@Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selArgs,
                    String sortOrder) {

            String tableName = null;
            switch(sURIMatcher.match(uri)){ //per adesso supportiamo solo le note
            case ENTI: 
                    tableName = DBHelper.TABLE_NAME;
                    break;
            default:
                    throw new IllegalArgumentException("Invalid Uri!");

            }

            Cursor cursor = db.query(tableName, projection, selection, 
                            selArgs, null, null, sortOrder);
            cursor.setNotificationUri(getContext().getContentResolver(), uri);

            return cursor;
    }
	
	@Override
    public Uri insert(Uri uri, ContentValues cv) {
            String tableName = null;
            switch(sURIMatcher.match(uri)){ //per adesso supportiamo solo le note
            case ENTI: 
                    tableName = DBHelper.TABLE_NAME;
                    break;
            default:
                    throw new IllegalArgumentException("Invalid Uri!");

            }

            long id = db.insert(tableName, null, cv);

            Uri retUri = ContentUris.withAppendedId(ENTI_CONTENT_URI, id);
            getContext().getContentResolver().notifyChange(retUri, null);

            return retUri;

    }
	
	@Override
	public int delete(Uri uri, String arg2, String[] arg3) {
		String tableName = null;
        switch(sURIMatcher.match(uri)){ //per adesso supportiamo solo le note
        case ENTI: 
                tableName = DBHelper.TABLE_NAME;
                break;
        default:
                throw new IllegalArgumentException("Invalid Uri!");

        }

        int id = db.delete(tableName, null, null);

        Uri retUri = ContentUris.withAppendedId(ENTI_CONTENT_URI, id);
        getContext().getContentResolver().notifyChange(retUri, null);

        return id;
	}
	
	@Override
	public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public SQLiteDatabase getDb() {
		return db;
	}
	
	public void setDb(SQLiteDatabase db) {
		this.db = db;
	}
}
